package com.gogowan.agroenterprise.widget.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.gogowan.agroenterprise.R;
import com.gogowan.agroenterprise.contorl.MyAsyncTask;
import com.gogowan.agroenterprise.model.LogsBean;
import com.gogowan.agroenterprise.model.LogsRequestBean;
import com.gogowan.agroenterprise.model.UserType;
import com.gogowan.agroenterprise.utils.Utils;

import org.json.JSONObject;

import java.util.List;

public class MainLogsAdapater extends BaseAdapter {
    private LayoutInflater inflater;
    private Context context;
    private List<LogsBean> list;
    public MainLogsAdapater(Context context, List<LogsBean> list){
        this.context=context;
        inflater= LayoutInflater.from(context);
        this.list=list;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Houlder houlder=null;
        if ( convertView==null){
            convertView=inflater.inflate(R.layout.mainlogs_adapter,null);
            houlder=new Houlder();
            houlder.DealerName_Tv=(TextView)convertView.findViewById(R.id.mainlist_item_Dealer_Name_Tv);
            houlder.Station_Tv=(TextView)convertView.findViewById(R.id.mainlist_item_station_Tv);
            houlder.Time_Tv=(TextView)convertView.findViewById(R.id.mainlist_item_Time_Tv);
            houlder.tv_leixing=(TextView)convertView.findViewById(R.id.tv_leixing);
            houlder.tv_caozuoren=(TextView)convertView.findViewById(R.id.tv_caozuoren);
            houlder.btn_caozuo =(Button) convertView.findViewById(R.id.tv_caozuo);
            convertView.setTag(houlder);
        }else {
            houlder=(Houlder) convertView.getTag();
        }
        houlder.DealerName_Tv.setText(list.get(position).getData());
        StringBuffer sb=new StringBuffer();
        sb.append("类型：");
        if((UserType._GONG_CHANG.getTypeID()+"").equals(list.get(position).getUsertype())){
            sb.append("工厂");
        }else{
            sb.append("经销商");
        }
        sb.append(list.get(position).getTypeString());
        houlder.tv_leixing.setText(sb.toString());
        houlder.tv_caozuoren.setText("操作人："+list.get(position).getUser());
        houlder.Time_Tv.setText("操作时间："+list.get(position).getTime());
        houlder.btn_caozuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendHttp(list.get(position));
            }
        });
        if (!"200".equals(list.get(position).getCode())){
            houlder.Station_Tv.setTextColor(context.getResources().getColor(R.color.red));
        }else {
            houlder.Station_Tv.setTextColor(context.getResources().getColor(R.color.text_02));
        }
        houlder.Station_Tv.setText("状态："+list.get(position).getMsg());
        return convertView;
    }

    private void sendHttp(LogsBean logsBean) {
        LogsRequestBean logsRequestBean=new LogsRequestBean();
        StringBuffer sb=new StringBuffer();
        if((UserType._GONG_CHANG.getTypeID()+"").equals(logsBean.getUsertype())){
            sb.append("工厂");
        }else{
            sb.append("经销商");
        }
        sb.append(logsBean.getTypeString());
        logsRequestBean.setType(sb.toString());
        logsRequestBean.setCode(logsBean.getData());
        LogVerifyAsyn logVerifyAsyn = new LogVerifyAsyn();
        logVerifyAsyn.execute(logsRequestBean);
    }
    /**
     * 退货校验
     *
     * @author tjh
     *
     */
    private class LogVerifyAsyn extends MyAsyncTask {

        @Override
        protected void onPostExecute(Object res) {

            //获取提交请求
            JSONObject data1 =null;
            if (res == null || res.equals("")) {
                Toast.makeText(context, context.getString(R.string.server_error), Toast.LENGTH_LONG).show();
            } else {
                try {
                    JSONObject jo=new JSONObject(res.toString());
                    Toast.makeText(context,jo.getString("msg"),
                            Toast.LENGTH_LONG).show();


                }catch (Exception e){
                    Toast.makeText(context,"返回值："+res,
                            Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }
            }
            super.onPostExecute(res);
        }

        @Override
        protected Object doInBackground(Object... params) {
            LogsRequestBean logsRequestBean = (LogsRequestBean) params[0];

            String res = Utils.postCommit(context.getString(R.string.first_url) + context.getString(R.string.submit),
                    logsRequestBean);
            return res;
        }
    }
    class Houlder{
        TextView DealerName_Tv,Station_Tv,Time_Tv,tv_caozuoren,tv_leixing;
        Button btn_caozuo;
    }
}
